package com.leetcode.LC;

import java.util.HashMap;

public class LC953 {
    public boolean isAlienSorted(String[] words, String order) {
        //map记录26个字母的顺序，然后逐个比较单词确定顺序

        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < order.length(); i++) {
            map.put(order.charAt(i), i);
        }

        for (int i = 0; i < words.length-1; i++) {
            if (compareWord(words[i],words[i+1],map)==false){
                return false;
            }
        }
        return true;

    }

    public boolean compareWord(String a, String b, HashMap<Character, Integer> map) {

        int count = 0;
        while (count < a.length() && count < b.length()) {

            if (a.charAt(count) == b.charAt(count)) {
                count++;
            } else if (map.get(a.charAt(count)) > map.get(b.charAt(count))) {
                return false;
            }else {
                return true;
            }
        }
        if (a.length() > b.length()) {
            return false;
        } else {
            return true;
        }
    }

    public static void main(String[] args) {
        System.out.println(new LC953().isAlienSorted(new String[]{"hello", "leetcode"}, "hlabcdefgijkmnopqrstuvwxyz"));
    }
}
